package demo_0423;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MySet {

    public static void main(String[] args) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        set1.add(1);
        set1.add(2);
        set1.add(3);
        set2.add(3);
        set2.add(4);
        set2.add(5);
        // 并集
        set1.addAll(set2);
        System.out.println(set1);
        // 交集
        set1.retainAll(set2);
        System.out.println(set1);
        // 差集
        set1.removeAll(set2);
        System.out.println(set1);


    }

    @SuppressWarnings("unused")
    public static void main1(String[] args) {
        Set<String> fruits = new HashSet<>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("grape");
        fruits.add("pear");
        fruits.add("orange");
        System.out.println(fruits);
        System.out.println(fruits.size());
        System.out.println(fruits.contains("apple"));
        fruits.remove("apple");
        System.out.println(fruits.contains("apple"));

        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        Iterator<String> iterator = fruits.iterator();
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            System.out.println(fruit);
        }

    }
}
